package main

import "math"

func findPeakElement(nums []int) int {
	if len(nums) == 1 {
		return 0
	}
	get := func(i int) int {
		if i < 0 || i >= len(nums) {
			return math.MinInt
		}
		return nums[i]
	}
	left := 0
	right := len(nums) - 1
	for left <= right {
		mid := (left + right) / 2
		if get(mid-1) < get(mid) && get(mid+1) < get(mid) {
			return mid
		}
		if get(mid-1) < get(mid) && get(mid) < get(mid+1) {
			left = mid + 1
		} else {
			right = mid - 1
		}
	}
	return right + 1
}

func main() {
	//println(findPeakElement([]int{1, 2, 3, 1}))
	//println(findPeakElement([]int{1, 2, 1, 3, 5, 6, 4}))
	println(findPeakElement([]int{1, 2, 1, 3, 5, 3, 4}))
}
